package com.xiejr.actuator.enums;

import com.alibaba.fastjson.JSON;
import com.xiejr.actuator.exceptioin.SocketActuatorException;
import com.xiejr.actuator.service.SystemService;
import com.xiejr.actuator.socket.model.Message;
import lombok.Getter;

import javax.websocket.Session;

/**
 * @program: socket-actuator-spring-boot-starter
 * @description: 消息枚举
 * @author: xjr
 * @create: 2020-07-02 10:14
 **/
@Getter
public enum MessageStrategyEnum {

    RUNTIME("1"){
        @Override
        public void done(Session session,SystemService systemService,String message) throws Exception {
            session.getBasicRemote().sendText(JSON.toJSONString(systemService.getRuntimeInfo()));
        }
    },
    CHECK_FACE("2"){
        @Override
        public void done(Session session, SystemService systemService,String message) throws Exception {
            session.getBasicRemote().sendText(Message.makeUpResult(()->{
                try {
                    return systemService.aiCheck(message);
                } catch (Exception e) {
                    throw new SocketActuatorException(e.getMessage());
                }
            }));
        }
    },
    SEARCH_FACE("3"){
        @Override
        public void done(Session session, SystemService systemService, String message) throws Exception {
            session.getBasicRemote().sendText(Message.makeUpResult(()->{
                try {
                    return systemService.aiSearch(message);
                } catch (Exception e) {
                    throw new SocketActuatorException(e.getMessage());
                }
            }));
        }
    };

    private String code;

    MessageStrategyEnum(String code){
        this.code=code;
    }

    public abstract void done(Session session, SystemService systemService,String message) throws Exception;

    public static MessageStrategyEnum getBycode(String name){
        MessageStrategyEnum[] strategyEnumss=MessageStrategyEnum.values();
        for (int i = 0; i <strategyEnumss.length ; i++) {
            if (strategyEnumss[i].getCode().equals(name)){
                return strategyEnumss[i];
            }
        }
        return null;
    }
}
